from django.db import models
from django.contrib.auth.models import User
from meetings.models import Group


class AITaskLog(models.Model):
    """
AI任务日志"""
    TASK_TYPE_CHOICES = [
        ('summary', '摘要'),
        ('recommend', '推荐'),
        ('match', '匹配'),
    ]
    
    STATUS_CHOICES = [
        ('success', '成功'),
        ('failed', '失败'),
    ]
    
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='ai_tasks', verbose_name='用户', db_comment='用户ID')
    task_type = models.CharField(max_length=50, choices=TASK_TYPE_CHOICES, verbose_name='任务类型', db_comment='任务类型:summary摘要,recommend推荐,match匹配')
    input_data = models.TextField(verbose_name='输入数据JSON', db_comment='输入数据JSON')
    output_data = models.TextField(verbose_name='输出数据JSON', db_comment='输出数据JSON')
    model_name = models.CharField(max_length=50, verbose_name='使用模型', db_comment='使用模型')
    tokens_used = models.IntegerField(default=0, verbose_name='Token消耗', db_comment='Token消耗')
    execution_time = models.FloatField(null=True, blank=True, verbose_name='执行时间秒', db_comment='执行时间(秒)')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, verbose_name='状态', db_comment='状态:success成功,failed失败')
    error_message = models.TextField(null=True, blank=True, verbose_name='错误信息', db_comment='错误信息')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_comment='创建时间')

    class Meta:
        verbose_name = 'AI任务日志'
        verbose_name_plural = 'AI任务日志'
        db_table = 'zhahuo_ai_task_log'
        db_table_comment = 'AI任务执行日志表'
        ordering = ['-created_at']

    def __str__(self):
        return f'{self.user.username} - {self.task_type} - {self.status}'


class AIMatchLog(models.Model):
    """智能匹配日志"""
    group = models.ForeignKey(Group, on_delete=models.CASCADE, related_name='match_logs', verbose_name='小组', db_comment='小组ID')
    task_title = models.CharField(max_length=200, verbose_name='任务标题', db_comment='任务标题')
    required_time = models.DateTimeField(verbose_name='需要时间', db_comment='需要时间')
    matched_users = models.TextField(verbose_name='匹配的用户JSON', db_comment='匹配的用户JSON')
    match_score = models.FloatField(null=True, blank=True, verbose_name='匹配分数', db_comment='匹配分数')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', db_comment='创建时间')

    class Meta:
        verbose_name = '智能匹配日志'
        verbose_name_plural = '智能匹配日志'
        db_table = 'zhahuo_ai_match_log'
        db_table_comment = '智能匹配日志表'
        ordering = ['-created_at']

    def __str__(self):
        return f'{self.group.name} - {self.task_title}'
